package com.UnionPayCenter.service.serviceImpl;

import com.UnionPayCenter.service.ServiceTemplate;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.Callable;

@Service
@Slf4j
public class ServiceTemplateImpl implements ServiceTemplate {

    @Override
    public <R> Page<R> pagination(Integer page, Integer pageSize, Callable callable) {
        try {
            if (page == null) {
                // 没有分页条件时，默认为1000
                page = 1;
                pageSize = 1000;
            }

            // 默认每页展示20条记录
            if (pageSize == null) {
                pageSize = 20;
            }
            Page<R> pageResult = PageHelper.startPage(page, pageSize);
            callable.call();
            return pageResult;
        } catch (Exception e) {
            log.error("invoke callable error: {}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
}
